package com.tiantian.quanminxiu.dialog;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.event.LiveRoomEvent;
import com.tiantian.quanminxiu.presenter.DialogUserInfoCardPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.DialogBase;
import com.tiantian.quanminxiu.utils.EventBusUtils;
import com.tiantian.quanminxiu.view.DialogUserInfoCardView;

import rx.functions.Action1;

/**
 * Created by admin on 2016/9/14.
 */
public class Dialog_UserInfoCard extends DialogBase implements DialogUserInfoCardView{

    private Activity activity;
    private int UserId;
    private int Fan;                    //用户没有粉丝。用于判断是否是主播
    private int level;
    private int anchorlevel;
    private String NickName;
    private boolean attention;
    private boolean IsLive;             //是否在直播间
    private DialogUserInfoCardPresent dialogGiftListPresent;
    private ImageView imgDialogIcon,imgUserLevel,imgAnchorLevel;
    private TextView txtUserName,txtUserID,txtFanRollCount,txtAtttentionSub;
    private View txtAdminSub,txtPresentSub,txtToldHimSub,txtPrivateChat,linDialogButtons;

    public Dialog_UserInfoCard(Activity activity, int themeResId,int UserId,String NickName,int fan,int level,int anchorlevel,boolean attention) {
        this(activity,themeResId, UserId, NickName, fan, level,anchorlevel, attention, true);
    }

    public Dialog_UserInfoCard(Activity activity, int themeResId,int UserId,String NickName,int fan,int level,int anchorlevel,boolean attention,boolean IsLive) {
        super(activity, themeResId);
        this.activity = activity;
        this.UserId = UserId;
        this.attention = attention;
        this.Fan = fan;
        this.level = level;
        this.NickName = NickName;
        this.IsLive = IsLive;
        this.anchorlevel = anchorlevel;
        InitView(activity);
        initUserInfo();
    }

    @Override
    protected void InitView(Activity activity) {
        setContentView(R.layout.dialog_userinfocard);
        Init(activity);

        dialogGiftListPresent = new DialogUserInfoCardPresent();
        dialogGiftListPresent.attachView(this);

        imgDialogIcon = (ImageView) findViewById(R.id.imgDialogIcon);
        imgUserLevel = (ImageView) findViewById(R.id.imgUserLevel);
        imgAnchorLevel = (ImageView) findViewById(R.id.imgAnchorLevel);
        linDialogButtons = findViewById(R.id.linDialogButtons);
        txtAdminSub = findViewById(R.id.txtAdminSub);
        txtPresentSub = findViewById(R.id.txtPresentSub);
        txtToldHimSub = findViewById(R.id.txtToldHimSub);
        txtPrivateChat = findViewById(R.id.txtPrivateChat);

        txtAtttentionSub = (TextView) findViewById(R.id.txtAtttentionSub);
        txtUserName = (TextView) findViewById(R.id.txtUserName);
        txtUserID = (TextView) findViewById(R.id.txtUserID);
        txtFanRollCount = (TextView) findViewById(R.id.txtFanRollCount);

        txtAtttentionSub.setVisibility(View.VISIBLE);
        txtFanRollCount.setVisibility(View.VISIBLE);
        if(Fan<0){
            txtAtttentionSub.setVisibility(View.GONE);
            txtFanRollCount.setVisibility(View.GONE);
        }
        if(!IsLive){
            linDialogButtons.setVisibility(View.GONE);
        }

        RxView.clicks(txtAdminSub).map(View->txtAdminSub).subscribe(OC_Button);
        RxView.clicks(txtAtttentionSub).map(View->txtAtttentionSub).subscribe(OC_Button);
        RxView.clicks(txtPresentSub).map(View->txtPresentSub).subscribe(OC_Button);
        RxView.clicks(txtToldHimSub).map(View->txtToldHimSub).subscribe(OC_Button);
        RxView.clicks(txtPrivateChat).map(View->txtPrivateChat).subscribe(OC_Button);
    }

    public Action1<View> OC_Button = v->{
        switch (v.getId()){
            case R.id.txtAdminSub:
                Dialog_AdminUser.show(relMain,activity,Dialog_UserInfoCard.this);
                break;
            case R.id.txtAtttentionSub:
                dialogGiftListPresent.attentionAnchor(UserId+"",attention);
                break;
            case R.id.txtPresentSub:
                if(Fan<0){
                    APPUtils.Dialog_GiftList_To(activity,relMain,-1,"");
                }else{
                    APPUtils.Dialog_GiftList_To(activity,relMain,UserId,NickName);
                }
                break;
            case R.id.txtToldHimSub:
                toldHim();
                break;
            case R.id.txtPrivateChat:
                privateChat();
                break;
        }
    };

    @Override
    public void initUserInfo() {
        //用户数据
        imgDialogIcon.post(new Runnable() {
            @Override
            public void run() {
                APPUtils.displayImage(APPUtils.createUserAvatar(UserId), imgDialogIcon, thisContext, imgDialogIcon.getWidth() / 2);
            }
        });
        txtUserID.setText("全民秀ID:" + UserId);
        txtUserName.setText(NickName);
        txtFanRollCount.setText("粉丝:"+Fan);
        imgUserLevel.setImageResource(APPUtils.getLevelPic(activity, level));
        imgAnchorLevel.setImageResource(APPUtils.getAnchorLevelPic(activity,anchorlevel));
        if(attention){
            txtAtttentionSub.setBackgroundResource(R.drawable.shape_radius_gray);
            txtAtttentionSub.setText("已关注");
        }
    }

    @Override
    public void adminSub() {

    }

    @Override
    public void RePortUser() {

    }

    @Override
    public void DefriendUser() {

    }

    @Override
    public void presentGift() {

    }

    @Override
    public void toldHim() {
        EventBusUtils.getInstance().post(new LiveRoomEvent("","toldHim",UserId,NickName));
        dismiss();
    }

    @Override
    public void privateChat() {
        EventBusUtils.getInstance().post(new LiveRoomEvent("","privateChat",UserId,NickName));
        dismiss();
    }

    @Override
    public void attentionAnchor() {
        attention = !attention;
        EventBusUtils.getInstance().post(new LiveRoomEvent(""+attention,"attentionAchor"));
        if(attention){
            txtAtttentionSub.setBackgroundResource(R.drawable.shape_radius_gray);
            txtAtttentionSub.setText("已关注");
        }else {
            txtAtttentionSub.setBackgroundResource(R.drawable.shape_radius_orange);
            txtAtttentionSub.setText("关注");
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        dialogGiftListPresent.detachView();
    }
}
